package com.vn.rainbow.exoalarm.fragment;

import java.util.Calendar;

import com.github.clans.fab.FloatingActionButton;
import com.vn.rainbow.exoalarm.R;
import com.vn.rainbow.exoalarm.activity.SetAlarmActivity;
import com.vn.rainbow.exoalarm.listener.DaySelectedListener;
import com.vn.rainbow.exoalarm.ui.CircularRevealView;
import com.vn.rainbow.exoalarm.ui.DaySelectedView;
import com.vn.rainbow.exoalarm.ui.TimeSetlectedView;
import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SetAlarmFragment extends BaseFragment implements OnClickListener,
		TimePickerDialog.OnTimeSetListener, DaySelectedListener {

	private FloatingActionButton fabSave;
	private TimeSetlectedView mTimeSelected;
	private DaySelectedView mDaySelectedView;
	private boolean is24HourFormat;
	private TextView tvTone;
	private String toneFomat;
	private TextView tvSnooze;
	private String snoozeFormat;
	private ImageView btnSound, btnVibrate;

	private StringBuilder dayRepeat;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_set_alarm, container,
				false);
		// TODO Auto-generated method stub
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mRevealView = (CircularRevealView) rootView.findViewById(R.id.reveal);

		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				show();
			}
		}, 100);

		dayRepeat = new StringBuilder();

		mTimeSelected = (TimeSetlectedView) rootView
				.findViewById(R.id.time_selected);
		mTimeSelected.setOnClickListener(this);

		mDaySelectedView = (DaySelectedView) rootView
				.findViewById(R.id.day_selected);
		mDaySelectedView.setDaySelectedListener(this);

		tvTone = (TextView) rootView.findViewById(R.id.tv_tone);
		toneFomat = mContext.getResources().getString(R.string.tone);
		setTone("Default");

		tvTone.setOnClickListener(this);

		tvSnooze = (TextView) rootView.findViewById(R.id.tv_snooze);
		snoozeFormat = mContext.getResources().getString(R.string.snooze);
		tvSnooze.setText(String.format(snoozeFormat, "Disable"));
		tvSnooze.setOnClickListener(this);

		btnSound = (ImageView) rootView.findViewById(R.id.btn_sound);
		btnSound.setOnClickListener(this);

		btnVibrate = (ImageView) rootView.findViewById(R.id.btn_vibrate);
		btnVibrate.setOnClickListener(this);

	}

	private void setTone(String str) {
		tvTone.setText(String.format(toneFomat, str));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		if (v == mTimeSelected) {
			Calendar now = Calendar.getInstance();
			is24HourFormat = DateFormat.is24HourFormat(mContext);
			TimePickerDialog tpd = TimePickerDialog.newInstance(this,
					now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE),
					is24HourFormat);
			tpd.setThemeDark(is24HourFormat);
			tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialogInterface) {
					Log.d("TimePicker", "Dialog was cancelled");
				}
			});
			tpd.show(getChildFragmentManager(), "Timepickerdialog");
		} else if (v == tvTone) {

		} else if (v == tvSnooze) {

		} else if (v == btnSound) {

		} else if (v == btnVibrate) {

		}
	}

	@Override
	public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
		// TODO Auto-generated method stub
		String am = "AM";
		if (hourOfDay >= 12) {
			am = "PM";
			// hourOfDay -= 12;
		}

		// if (!is24HourFormat && hourOfDay == 0)
		// hourOfDay = 12;

		if (!is24HourFormat) {

			if (hourOfDay > 12)
				hourOfDay -= 12;
			else if (hourOfDay == 0)
				hourOfDay = 12;
		}

		String hourString = Integer.toString(hourOfDay);
		if (hourString.length() == 1)
			hourString = "0" + hourString;

		String minuteString = Integer.toString(minute);
		if (minuteString.length() == 1)
			minuteString = "0" + minuteString;

		mTimeSelected.setChangeTime(hourString + ":" + minuteString, am,
				is24HourFormat);

	}

	@Override
	public void onDaySelected(int id) {
		// TODO Auto-generated method stub
		for (int i = 0; i < dayRepeat.length(); i++)
			if (Integer.parseInt(dayRepeat.charAt(i) + "") == id) {
				dayRepeat.deleteCharAt(i);
				return;
			}

		dayRepeat.append(id);
		Toast.makeText(mContext, dayRepeat.toString(), 1000).show();

	}
}
